<template>
	<view class="content">
		<view class="list">
			<view class="list_fl">头像</view>
			<view class="list_fr">
				<image class="userAvatar" :src="userInfo.avatar" mode="" @click="goAvatar"></image>
				<view class="iconfont iconavatar">&#xe601;</view>
			</view>
		</view>
		<view class="list" @click="getNickname">
			<view class="list_fl">昵称</view>
			<view class="list_fl">{{userInfo.nickname}}</view>
		</view>
	</view>
</template>

<script>
	import urlConfig from '../../../../common/api/config.js'
	import api from "../../../../common/api/api.js"
	export default {
		data() {
			return {
				userInfo:{}
			}
		},
		onLoad() {
			// let userInfo = JSON.parse(uni.getStorageSync(this.constant.StorageName.APP_USER_INFO));
			//  this.userInfo = userInfo
			//  console.log(userInfo)
			//  let that = this;
			//  uni.$on(that.constant.EventKey.UPDATE_USER_INFO,function(data){
			//  	let userInfo = JSON.parse(uni.getStorageSync(this.constant.StorageName.APP_USER_INFO));
			//  	that.userInfo = userInfo;
			//  })
			this.getUserInfo()
		},
		methods: {
			
			getUserInfo: function() {
				api.getUserInfo().then(res => {
					console.log(res)
					this.userInfo = res.data;
				})
			},
			// 设置昵称
			getNickname: function () {
				uni.navigateTo({
					url: 'setNickname/setNickname',
				});
			},
			// 上传头像
			goAvatar: function () {
				let that = this;
				uni.chooseImage({
				    count: 1, //默认9
				    sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				    sourceType: ['album'], //从相册选择
				    success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.uploadFile({
							url: urlConfig + 'uploadService', //接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							success: (res) => {
								console.log(res); 
								var data = JSON.parse(res.data);
								// console.log(data,"======")
								// that.weixinCode = data.data.data
								let avatar = data.data.data;
								api.changeUsewrInfo({
									avatar:avatar
								}).then(res => {
									console.log(res)
									// that.tui.toast(res.data)
									uni.showToast({
										title:res.data,
										icon:"none"
									})
									setTimeout(function () {
										// that.cacheHelper.updateUserInfo();
										uni.$emit('login',{msg:'更新信息'})
									},500)
								})
								
							}
						});
					}
				});	
			}
		}
	}
</script>

<style lang="less">
	page {
		width: 100%;
		box-sizing: border-box;
		letter-spacing: 3rpx;
		background: #F5F5F5;
		font-family: PingFang SC;
		padding: 20rpx 0;
		font-size: 30rpx;
		color: #140005;
	}
	.content {
		width: 100%;
		box-sizing: border-box;
	}
	.list {
		width: 100%;
		box-sizing: border-box;
		background: #FFFFFF;
		padding: 20rpx 30rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;
		.list_fr {
			display: flex;
			justify-content: flex-start;
			align-items: center;
			.userAvatar {
				width: 98rpx;
				height: 98rpx;
				border-radius: 50%;
			}
			.iconavatar {
				width: 50rpx;
				box-sizing: border-box;
				text-align: center;
				font-size: 28rpx;
				color: #999999;
			}
			
		}
	}

</style>
